package com.ehualu.esresult;

import com.alibaba.fastjson.JSON;
import com.ehualu.esresult.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Author: 吴敬超
 * @Date: 2020/7/16 17:33
 */
public class RuningCar {

    private static final Logger LOG = LoggerFactory.getLogger(RuningCar.class);

    public static void main(String[] args) {

        while (true) {

            LOG.info("*******************循环执行函数*************************");

            runing();

            try {
                /*一小时执行一次*/
                /*24 * 60 * 60 * 1000;*/
                Thread.sleep(1 * 60 * 60 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }

    }

    public static void runing() {

        LoadProperties loadProc = LoadProperties.getInstance();

        String postjosn = loadProc.getValues("search.count", "");

        String posturl = ESurlUtil.getUrl("index.vehicle");
        if (posturl.equals("")) {
            LOG.info("posturl 是空，不继续执行");


            return;
        }


        String result = HttpUtil.HttpPostWithJson(posturl, postjosn);


        if (result.equals("DefaultReturnValue")) {

            LOG.info("ES请求结果是空，不继续执行");
            return;

        }
        LOG.info("result is :");
        LOG.info(result);

        Integer countnum = JSON.parseObject(result)
                .getJSONObject("aggregations")
                .getJSONObject("stats_monthly_sales")
                .getInteger("count");

        LOG.info("countnum is :" + countnum);

        MysqlUtil mysqlUtil = new MysqlUtil();
        mysqlUtil.updateRuningcarnum(countnum);
    }
}
